﻿using System;
using System.Globalization;
using System.IO;
using QuanLyHopDong.Biz;
using QuanLyHopDong.Biz.Model;
using Resources;

namespace QuanLyHopDong.Web.HopDong
{
    public partial class PhieuXuatKhoNew : System.Web.UI.Page
    {
        #region Properties

        private HopDongBiz _hopDongBiz;
        private HopDongBiz HopDongManager
        {
            get { return _hopDongBiz ?? (_hopDongBiz = new HopDongBiz()); }
        }

        private int _hopDongId;
        private int HopDongId
        {
            get
            {
                if (_hopDongId <= 0)
                    int.TryParse(Request.QueryString["hopDongId"], out _hopDongId);
                return _hopDongId;
            }
        }

        private HopDongView _hopDongView;
        private HopDongView CurrentHopDongView
        {
            get
            {
                if (_hopDongView == null && HopDongId > 0)
                {
                    _hopDongView = HopDongManager.GetById(HopDongId);
                }
                return _hopDongView;
            }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (CurrentHopDongView == null)
                {
                    lblErrorMessage.Text = string.Format("{0}: Id={1}", GlobalStrings.NotExisted, HopDongId);
                    btnSave.Visible = false;
                    return;
                }

                LoadHopDongData();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;

            #region collect inputs and validate

            var phieuXuatKhoNew = new PhieuXuatKho
            {
                HopDongId = HopDongId,
                SoPhieu = txtSoPhieu.Text.Trim(),
                GhiChu = txtGhiChu.Text
            };

            DateTime ngayChungTu;
            if (DateTime.TryParseExact(txtNgayChungTu.Text.Trim(), GlobalStrings.DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out ngayChungTu))
            {
                phieuXuatKhoNew.NgayChungTu = ngayChungTu;
            }

            var filePhieuXuatName = string.Empty;
            if (filePhieuXuatKho.HasFile)
            {
                filePhieuXuatName = Path.GetFileName(filePhieuXuatKho.PostedFile.FileName);
                phieuXuatKhoNew.PhieuXuatKho1 = filePhieuXuatName;
            }

            var fileBienBanName = string.Empty;
            if (fileBienBanBanGiao.HasFile)
            {
                fileBienBanName = Path.GetFileName(fileBienBanBanGiao.PostedFile.FileName);
                phieuXuatKhoNew.BienBanBanGiao = fileBienBanName;
            }

            // todo: create phieu xuat kho
            new PhieuXuatKhoBiz().CreatePhieuXuat(phieuXuatKhoNew);

            // todo: upload files
            if (filePhieuXuatKho.HasFile)
            {
                var folderPath = Server.MapPath(string.Format("~/Files/HopDong/{0}", HopDongId));
                // create folder
                if (!Directory.Exists(folderPath))
                    Directory.CreateDirectory(folderPath);

                // save file
                var filePathFull = folderPath + "/" + filePhieuXuatName;
                if (File.Exists(filePathFull))
                    File.Delete(filePathFull);
                filePhieuXuatKho.PostedFile.SaveAs(filePathFull);
            }

            if (fileBienBanBanGiao.HasFile)
            {
                var folderPath = Server.MapPath(string.Format("~/Files/HopDong/{0}", HopDongId));
                // create folder
                if (!Directory.Exists(folderPath))
                    Directory.CreateDirectory(folderPath);

                // save file
                var filePathFull = folderPath + "/" + fileBienBanName;
                if (File.Exists(filePathFull))
                    File.Delete(filePathFull);
                fileBienBanBanGiao.PostedFile.SaveAs(filePathFull);
            }

            #endregion

            Response.Redirect(string.Format("KhoiLuong.aspx?hopDongId={0}#tabs-2", HopDongId));
        }

        #endregion

        #region Helpers

        public void LoadHopDongData()
        {
            ltrTenDonVi.Text = CurrentHopDongView.TenDonVi;
            ltrSoHopDong.Text = CurrentHopDongView.SoHopDong;
            ltrLoaiDuAn.Text = CurrentHopDongView.LoaiDuAn;
        }

        #endregion
    }
}